//app.js

//电商正式 appid wxf02b6affdf44fe96
//充电正式 appid wx2de41e63e4028fd1
//测试 appid wx4fdef76b25e48ce2
const  base="https://magtshop.svfucd.com"; //配置
// const  base="https://yutai.svfucd.com"; //配置
App({
   
    globalData: {
        access_token: "", //token暂存
        share_member: "" //token暂存
    },
    post(url, data, callback) {
        const _this=this;
        let fullUrl = `${base}${url}`
        let token = wx.getStorageSync('token') ? wx.getStorageSync('token') : ''
        wx.showLoading({
            title: "加载中..."
        });
        return new Promise((resolve, reject) => {
            wx.request({
                url: fullUrl,
                method: "POST",
                data,
                header: {
                    'content-type': "application/json;charset=utf-8",
                    'access_token': token,
                },
                success: (res) => {
                    console.log(res);
                    if (res.statusCode == 200) {
                        if (callback) {
                            callback(res.data);
                            return
                        }
                        if (res.data.result == 1) {
                            resolve(res.data);
                        } else {
                            wx.showToast({
                                title: res.data.message,
                                icon: 'none'
                            })
                        }
                    } else if (res.statusCode == 403) {
                        wx.login({
                            success(res) {
                                if (res.code) {
                                    let datas = {
                                        js_code: res.code,
                                    }
                                    wx.request({
                                        url: `${base}/auth/ma/token`,
                                        method: "POST",
                                        data: datas,
                                        header: {
                                            'content-type': "application/json;charset=utf-8"
                                        },
                                        success: (res) => {
                                            if (res.data.result == 1) {
                                                wx.setStorageSync('token', res.data.data.access_token);
                                                _this.post(url, data, resolve);
                                            } else {
                                                wx.navigateTo({
                                                    url: '/pages/login/index',
                                                })
                                            }
                                        },
                                        fail: (err) => {
                                            console.log(err);
                                            wx.showToast({
                                                title: '接口请求错误',
                                                icon: 'none'
                                            })
                                            reject('接口请求错误')
                                        }
                                    })
                                } else {
                                    console.log('登录失败！' + res.errMsg)
                                }
                            }
                        })
                    } else {
                        wx.showToast({
                            title: res.data.message,
                            icon: 'none'
                        })
                    }

                },
                fail: () => {
                    wx.showToast({
                        title: '接口请求错误',
                        icon: 'none'
                    })
                    reject('接口请求错误')
                },
                complete: () => {
                    setTimeout(() => {
                        wx.hideLoading()
                    }, 100)
                }
            })
        })
    },
    get(url, data, callback) {
        const _this=this;
        let fullUrl = `${base}${url}`
        let token = wx.getStorageSync('token') ? wx.getStorageSync('token') : ''
        wx.showLoading({
            title: "加载中"
        });
        return new Promise((resolve, reject) => {
            wx.request({
                url: fullUrl,
                method: "GET",
                data,
                header: {
                    'content-type': "application/json;charset=utf-8",
                    'access_token': token,
                },
                success: (res) => {
                    if (res.statusCode == 200) {
                        if (callback) {
                            callback(res.data);
                            return
                        }
                        if (res.data.result == 1) {
                            resolve(res.data);
                        } else {
                            wx.showToast({
                                title: res.data.message,
                                icon: 'none'
                            })
                        }
                    } else if (res.statusCode == 403) {
                        wx.login({
                            success(res) {
                                if (res.code) {
                                    let datas = {
                                        js_code: res.code,
                                    }
                                    wx.request({
                                        url: `${base}/auth/ma/token`,
                                        method: "POST",
                                        data: datas,
                                        header: {
                                            'content-type': "application/json;charset=utf-8"
                                        },
                                        success: (res) => {
                                            if (res.data.result == 1) {
                                                wx.setStorageSync('token', res.data.data.access_token);
                                                _this.get(url, data, resolve);
                                            } else {
                                                wx.navigateTo({
                                                    url: '/pages/login/index',
                                                })
                                            }
                                        },
                                        fail: () => {
                                            wx.showToast({
                                                title: '接口请求错误',
                                                icon: 'none'
                                            })
                                            reject('接口请求错误')
                                        }
                                    })
                                } else {
                                    console.log('登录失败！' + res.errMsg)
                                }
                            }
                        })
                    } else {
                        wx.showToast({
                            title: res.data.message,
                            icon: 'none'
                        })
                    }

                },
                fail: () => {
                    wx.showToast({
                        title: '接口请求错误',
                        icon: 'none'
                    })
                    reject('接口请求错误')
                },
                complete: () => {
                    setTimeout(() => {
                        wx.hideLoading()
                    }, 100)
                }
            })
        })
    },




















    dateFormat(date, fmt) {
        let ret;
        const opt = {
            "Y+": date.getFullYear().toString(), // 年
            "M+": (date.getMonth() + 1).toString(), // 月
            "D+": date.getDate().toString(), // 日
            "h+": date.getHours().toString(), // 时
            "m+": date.getMinutes().toString(), // 分
            "s+": date.getSeconds().toString() // 秒
        };
        for (let k in opt) {
            ret = new RegExp("(" + k + ")").exec(fmt);
            if (ret) {
                fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
            };
        };
        return fmt;
    }
})